package org.example;

import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * Hello world!
 *
 */
public class App
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );


        BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(10);
        //生产者：不断生产自增的num；
        Thread t1 = new Thread(()->{
            int num = 0;
            while (true){
                try {
                    queue.put(num);
                    System.out.println("生产者：" + num);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                num++;
            }
        });

        //消费者：每隔1秒消费一个num；
        Thread t2 = new Thread(()->{
            while (true){
                try {
                    System.out.println("消费者：" + queue.take());
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t1.start();
        t2.start();

    }
}
